<?php
namespace app\admin\controller;
use app\model\ExportTask as ExportTaskModel;
use app\model\User as UserModel;
use app\model\Admin as AdminModel;
use think\Db;


class ExportTask extends Base
{
    public function __construct(){
        $model = new ExportTaskModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '导出任务';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'status','name'=>'状态','type'=>'select2_1','params'=>$model->getStatusList()],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'name','type'=>'like'],
        ];
        $map = getSearchParam($map_params);
        $map[] = ['admin_id','=',$this->login_user['id']];

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();

        foreach ($list['data'] as $k => $vo) {
            // $list['data'][$k]['admin'] = Db::name('admin')->where('id',$vo['admin_id'])->find();
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }


    public function delete($id){
        $model = $this->model;
        $task = $model->where('id',$id)->find();
        if ( $model->where('id',$id)->delete() ) {
            $zip_path = './task/task_zip/'.$task['task_sn'].'.zip';
            $folder = './task/'.$task['task_sn'];
            if ( file_exists($folder) ) {
                del_dir($folder);
            }
            if ( file_exists($zip_path) ) {
                unlink($zip_path);
            }

            $this->success("删除成功");
        }else{
            $this->error("删除失败");
        }
    }

   
}
